package com.mifengs.mall.ui.hxcec;

import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;

import com.hyphenate.chat.ChatClient;
import com.hyphenate.chat.Message;
import com.hyphenate.helpdesk.easeui.recorder.MediaManager;
import com.hyphenate.helpdesk.easeui.ui.BaseActivity;
import com.hyphenate.helpdesk.easeui.ui.ChatFragment;
import com.hyphenate.helpdesk.easeui.util.CommonUtils;
import com.hyphenate.helpdesk.easeui.util.Config;
import com.mifengs.commonbase.util.PreferenceUtils;
import com.mifengs.mall.Constant;
import com.mifengs.mall.R;
import com.mifengs.mall.ui.main.MainActivity;

public class ChatActivity extends BaseActivity {

    public static ChatActivity instance = null;

    private ChatFragment chatFragment;

    String toChatUsername;
    private String orderName ="";
    private String orderPrice ="";
    private String orderImg ="";
    private String orderUrl ="";
    private String orderId = "";
    private int type = 0;
    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        setContentView(R.layout.hd_activity_chat);
        instance = this;
        //IM服务号
        toChatUsername = Constant.DEFAULT_CUSTOMER_ACCOUNT;
        orderName =getIntent().getStringExtra("orderName");
        orderPrice =getIntent().getStringExtra("orderPrice");
        orderImg =getIntent().getStringExtra("orderImg");
        orderUrl =getIntent().getStringExtra("orderUrl");
        orderId = getIntent().getStringExtra("orderId");
        type = getIntent().getIntExtra("type",0);
        //可以直接new ChatFragment使用
        String chatFragmentTAG = "chatFragment";
        chatFragment = (ChatFragment) getSupportFragmentManager().findFragmentByTag(chatFragmentTAG);
        if (chatFragment == null){
            chatFragment = new CustomChatFragment();
            //传入参数
            chatFragment.setArguments(getIntent().getExtras());
            getSupportFragmentManager().beginTransaction().add(R.id.container, chatFragment, chatFragmentTAG).commit();
            String getOrderId= PreferenceUtils.getOrderId(getApplicationContext());

            if(type == 1){
                if("".equals(getOrderId)){
                    sendOrderOrTrack();
                    PreferenceUtils.saveOrderId(getApplicationContext(),orderId);
                }else{
                    if(!getOrderId.equals(orderId)){
                        sendOrderOrTrack();
                        PreferenceUtils.saveOrderId(getApplicationContext(),orderId);
                    }
                }

            }

        }
    }


    /**
     * 发送订单或轨迹消息
     */
    private void sendOrderOrTrack() {
        sendOrderMessage();
    }

    /**
     * 发送订单消息
     *
     * 不发送则是saveMessage
     * @para"
     */
    private void sendOrderMessage(){
        Message message = Message.createTxtSendMessage(getString(R.string.app_name), toChatUsername);
        message.addContent(DemoMessageHelper.createOrderInfo(this,"",orderPrice,orderName,orderImg,orderUrl));
        ChatClient.getInstance().chatManager().saveMessage(message);
    }

    /**
     * 发送轨迹消息
     * @param
     */
    private void sendTrackMessage() {
        Message message = Message.createTxtSendMessage(getString(R.string.app_name), toChatUsername);
        message.addContent(DemoMessageHelper.createVisitorTrack(this,"",orderPrice,orderName,orderImg,orderUrl));
        ChatClient.getInstance().chatManager().sendMessage(message);
    }

//    private String getMessageContent(int selectedIndex){
//        switch (selectedIndex){
//            case 1:
//                return getResources().getString(R.string.em_example1_text);
//            case 2:
//                return getResources().getString(R.string.em_example2_text);
//            case 3:
//                return getResources().getString(R.string.em_example3_text);
//            case 4:
//                return getResources().getString(R.string.em_example4_text);
//        }
//        // 内容自己随意定义。
//        return "";
//    }



    @Override
    protected void onDestroy() {
        super.onDestroy();
        MediaManager.release();
        instance = null;
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        // 点击notification bar进入聊天页面，保证只有一个聊天页面
        String username = intent.getStringExtra(Config.EXTRA_SERVICE_IM_NUMBER);
        if (toChatUsername.equals(username))
            super.onNewIntent(intent);
        else {
            finish();
            startActivity(intent);
        }

    }

    @Override
    public void onBackPressed() {
        if (chatFragment != null) {
            chatFragment.onBackPressed();
        }
        if (CommonUtils.isSingleActivity(this)) {
            Intent intent = new Intent(this, MainActivity.class);
            startActivity(intent);
        }
        super.onBackPressed();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }

}
